package com.ruoyi.web.controller.laiqianba.merchantdata;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.PhoneUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.service.IMerchantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.MerchantUserInfo;
import com.ruoyi.merchantdata.service.IMerchantUserInfoService;
import com.ruoyi.web.wrapper.MerchantUserInfoWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.core.domain.AjaxResult.error;

/**
 * 商户成员信息Controller
 *
 * @author ruoyi
 * @date 2023-09-02
 */
@RestController
@RequestMapping("/merchantdata/userInfo")
public class MerchantDataUserInfoController extends BaseController {

    @Autowired
    private IMerchantService iMerchantService;

    @Autowired
    private IMerchantUserInfoService merchantUserInfoService;


    /**
     * 查询商户成员信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:userInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantUserInfo merchantUserInfo) {
        startPage();
        List<Map<String, Object>> list = merchantUserInfoService.selectMerchantUserInfoMapList(merchantUserInfo);
        list = new MerchantUserInfoWrapper(list).wrap();
        return getDataTable(list);
    }


    /**
     * 新增商户成员信息
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:userInfo:add')")
    @Log(title = "商户成员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantUserInfo merchantUserInfo) {

        boolean isMobile = PhoneUtil.isMobile(merchantUserInfo.getPhone());
        if (!isMobile) {
            return AjaxResult.error("请填写大陆手机号");
        }
        return iMerchantService.insertMerchatUser(merchantUserInfo);
    }

    /**
     * 修改状态
     *
     * @param merchantUserInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:userInfo:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MerchantUserInfo merchantUserInfo) {
        return toAjax(merchantUserInfoService.updateMerchantUserInfo(merchantUserInfo));
    }

    @GetMapping("/all/list")
    public TableDataInfo Alllist(MerchantUserInfo merchantUserInfo) {
        List<Map<String, Object>> list = merchantUserInfoService.selectMerchantUserInfoMapList(merchantUserInfo);
        list = new MerchantUserInfoWrapper(list).wrap();
        return getDataTable(list);
    }


    /**
     * 删除公海数据分配
     */
    @PreAuthorize("@ss.hasPermi('merchantdata:userInfo:remove')")
    @Log(title = "公海数据分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        int i = merchantUserInfoService.deleteMerchantUserInfoById(id);
        if (i <= 0) {
            return AjaxResult.error("删除失败，请联系管理员");
        }
        return iMerchantService.deleUser(id);
    }

}
